package cn.zhx.xa.system.controller;


import cn.zhx.xa.model.dto.user.LoginDto;
import cn.zhx.xa.model.dto.user.UserRegisterDto;
import cn.zhx.xa.model.vo.common.Result;
import cn.zhx.xa.model.vo.common.ResultCodeEnum;
import cn.zhx.xa.model.vo.index.LoginVo;
import cn.zhx.xa.model.vo.index.SysUserInfoVo;
import cn.zhx.xa.model.vo.index.ValidateCodeVo;
import cn.zhx.xa.model.vo.menu.SysMenuVo;
import cn.zhx.xa.service.annotation.RequestLimit;
import cn.zhx.xa.system.service.SysMenuService;
import cn.zhx.xa.system.service.SysUserService;

import cn.zhx.xa.system.service.ValidateCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/system/index")
@Tag(name = "基础接口")
public class IndexController {
    @Resource
    private SysUserService sysUserService ;
    @Resource
    private ValidateCodeService validateCodeService;
    @Resource
    private SysMenuService sysMenuService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<LoginVo> login(@Valid @RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo, ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 用户注册
     *
     * @param userRegisterDto
     * @return 新用户id
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result<Long> userRegister(@Valid @RequestBody UserRegisterDto userRegisterDto){
        String username = userRegisterDto.getUsername();
        String password = userRegisterDto.getPassword();
        String checkPassword = userRegisterDto.getCheckPassword();
        sysUserService.userRegister(username, password, checkPassword);
        return Result.build(null, ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("/generateValidateCode")
    @Operation(summary = "获取验证码")
    @RequestLimit(count = 10)
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("/auth/getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result<SysUserInfoVo> getUserInfo(@RequestHeader(name = "token") String token) {
        return Result.build(sysUserService.getUserInfo(token), ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("/auth/logout")
    @Operation(summary = "用户退出登录")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    @GetMapping("/auth/menus")
    @Operation(summary = "获取用户菜单")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }


}
